#include "nameserver/RouteInfoManager.h"

namespace zeroplus::mq
{
    RouteInfoManager::RouteInfoManager()
    {
    }

    RouteInfoManager::~RouteInfoManager()
    {
    }

    std::string RouteInfoManager::RegisterRouteInfo(
        std::string clusterName,
        std::string brokerAddr,
        std::string brokerName,
        uint32_t brokerId,
        std::map<std::string, routeinfo::TopicData> topicDataTable)
    {
        // LOG_DEBUG("clusterName=%s, brokerAddr=%s, brokerName=%s, brokerId=%d, topicDataTableSize=%d",
        //           clusterName.c_str(), brokerAddr.c_str(), brokerName.c_str(), brokerId, topicDataTable.size());

        // 更新集群信息表。判断 Broker 所属集群是否存在，不存在则创建集群，然后将 Broker 名加入集群信息表
        auto clusterAddrTableIterator = this->clusterAddrTable_.find(clusterName);
        if (clusterAddrTableIterator == this->clusterAddrTable_.end())
        {
            std::set<std::string> brokerNames;
            this->clusterAddrTable_[clusterName] = brokerNames;
            LOG_DEBUG("集群信息表已更新");
        }
        this->clusterAddrTable_[clusterName].insert(brokerName);

        // 更新 Broker 地址表
        auto brokerDataIt = this->brokerAddrTable_.find(brokerName);
        routeinfo::BrokerData brokerData;
        if (brokerDataIt == brokerAddrTable_.end())
        {
            brokerData.set_cluster_name(clusterName);
            brokerData.set_broker_name(brokerName);
            auto broker_addrs = brokerData.mutable_broker_addrs();
            (*broker_addrs)[brokerId] = brokerAddr;
            brokerAddrTable_[brokerName] = brokerData;
            LOG_DEBUG("Broker地址表已更新, %d", brokerAddrTable_.size());
        }
        else
        {
            brokerData = brokerDataIt->second;
        }

        // 更新主题队列映射表
        for (auto &pair : topicDataTable)
        {
            std::string topic = pair.first;
            routeinfo::TopicData topicData = pair.second;
            routeinfo::QueueData queueData;
            queueData.set_queue_num(topicData.queue_num());
            queueData.set_broker_name(brokerName);
            if (topicQueueTable_.find(topic) == topicQueueTable_.end())
            {
                std::vector<routeinfo::QueueData> queueDataList;
                queueDataList.push_back(queueData);
                topicQueueTable_[topic] = queueDataList;
                LOG_DEBUG("主题队列映射表已更新");
                // LOG_DEBUG("主题队列映射表长度: %d", topicQueueTable_.size());
            }
            else
            {
                bool isUpdate = true;
                for (const auto &unitQueueData : topicQueueTable_.find(topic)->second)
                {
                    if (unitQueueData.broker_name() == queueData.broker_name())
                    {
                        isUpdate = false;
                    }
                }
                if (isUpdate)
                {
                    auto topicQueueIt = topicQueueTable_.find(topic);
                    topicQueueIt->second.push_back(queueData);
                }
            }
            // LOG_DEBUG("topic: %s QueueData长度: %d", topic.c_str(), topicQueueTable_.find(topic)->second.size());
        }

        return "ok";
    }

    std::string RouteInfoManager::UnRegisterRouteInfo() {}

    routeinfo::TopicRouteInfo RouteInfoManager::QueryRouteInfoByTopic(const std::string &topic)
    {
        routeinfo::TopicRouteInfo topicRouteData;
        std::set<std::string> brokerNameSet;
        // LOG_DEBUG("[]主题队列映射表长度: %d", topicQueueTable_.size());
        auto queueDataListIt = topicQueueTable_.find(topic);
        if (queueDataListIt != topicQueueTable_.end())
        {
            for (const auto &queueData : queueDataListIt->second)
            {
                topicRouteData.add_queue_data_list()->CopyFrom(queueData);
                brokerNameSet.insert(queueData.broker_name());
            }
            for (auto brokerName : brokerNameSet)
            {
                auto brokerNameIt = brokerAddrTable_.find(brokerName);
                if (brokerNameIt != brokerAddrTable_.end())
                {
                    topicRouteData.add_broker_data_list()->CopyFrom(brokerNameIt->second);
                }
            }
        }
        else
        {
            // LOG_DEBUG("topic %s, topicQueueTableSize: %d", topic.c_str(), topicQueueTable_.size());
        }
        return topicRouteData;
    }

    routeinfo::ClusterInfo RouteInfoManager::GetAllClusterInfo()
    {
        routeinfo::ClusterInfo clusterInfo;
        // 设置 broker_addr_table
        for (const auto &brokerEntry : brokerAddrTable_)
        {
            std::string brokerName = brokerEntry.first;
            routeinfo::BrokerData brokerData = brokerEntry.second;
            (*clusterInfo.mutable_broker_addr_table())[brokerName] = brokerData;
        }
        // 设置 cluster_addr_table
        for (const auto &clusterEntry : clusterAddrTable_)
        {

            std::string clusterName = clusterEntry.first;
            std::set<std::string> brokerNames = clusterEntry.second;
            // 创建一个 BrokerNameSet 消息
            routeinfo::BrokerNameSet brokerNameSet;
            for (std::string brokerName : brokerNames)
            {
                brokerNameSet.add_broker_data_set(brokerName);
            }
            // 将 BrokerNameSet 添加到 cluster_addr_table 中
            (*clusterInfo.mutable_cluster_addr_table())[clusterName] = brokerNameSet;
        }
        return clusterInfo;
    }

}
